<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class ExperConsAdd extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'age' => 'required',
            'sex' => [
                'required', function($attribute, $value, $fail) {
                    if (!in_array($value, [1, 2])) {
                        $fail('性别参数错误');
                        return;
                    }
                }
            ],
            'height' => 'required',
            'weight' => 'required',
            'sym_name' => 'required',
            'sym_desc' => 'required',
            'to_user_id' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => '请输入姓名',
            'age.required' => '请输入年龄',
            'sex.required' => '请输入性别',
            'height.required' => '请输入身高',
            'weight.required' => '请输入体重',
            'sym_name.required' => '请选择症状',
            'sym_desc.required' => '请输入症状描述',
            'to_user_id.required' => '请选择专家',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            "code"  => 500,
            "msg"   => $validator->getMessageBag()->first(),
            "data"  => null,
        ], 200, [
            'Content-Type'=>'application/json; charset=UTF-8'
        ]));
    }
}
